/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wargame.dao;

import wargame.entity.*;
import connectionmanager.DB;

/**
 *
 * @author stanley.2007
 */
public class GameLogDAO {

    public static GameLog retrieveGameLog(int game_log_id) {
        String sql = "SELECT `game_log_id`, `player_id`, `description`, `turn`, `timestamp` FROM `game_log` WHERE game_log_id = '" + game_log_id + "'";
        DB db = new DB();
        db.connect();
        db.query(sql);
        if (db.next()) {
            GameLog gl = new GameLog();
            gl.setGame_log_id(db.getInt("game_log_id"));
            gl.setPlayer_id(db.getInt("player_id"));
            gl.setDescription(db.getString("description"));
            gl.setTurn(db.getInt("turn"));
            gl.setTimestamp(db.getTimestamp("timestamp"));
            return gl;

        } else {
            return null;
        }
    }

    public static int insertGameLog(GameLog gl) {
        String sql = "INSERT INTO game_log (`player_id`, `description`, `turn`, `timestamp`) VALUES ('"+
                gl.getPlayer_id()+"', '"+gl.getDescription()+"', '"+
                gl.getTurn()+"', '"+gl.getTimestamp()+"');";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        int lastInsertedId = db.getLastInsertedID();
        db.close();
        return lastInsertedId;
    }

    public static void updateGameLog(GameLog gl) {
        String sql = "UPDATE game_log SET `player_id` = '"+gl.getPlayer_id()+"', `description` = '"+gl.getDescription()+"', `turn` = '"+gl.getTurn()+"', `timestamp` = '"+gl.getTimestamp()+"' WHERE game_log_id = '"+gl.getGame_log_id()+"';";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }

    public static void clear() {
        String sql = "TRUNCATE TABLE game_log";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }
}
